package org.iabako.client.ui.form.business.notification;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.CheckboxBoolean;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.Slider;
import org.iabako.client.ui.layout.template.Popup;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.NotificationConfig;
import org.iabako.shared.iservice.business.NotificationServiceAsync;

import java.util.List;

/**
 * Created by jose on 17/03/15.
 */
public abstract class NotificationConfigForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, NotificationConfigForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final NotificationServiceAsync notificationServiceAsync = NotificationServiceAsync.Util.getInstance();

    @UiField
    CheckboxBoolean stockRupture;
    @UiField
    CheckboxBoolean newPastExpenses;
    @UiField
    CheckboxBoolean newPastInstallments;
    @UiField
    CheckboxBoolean paymentRequest;
    @UiField
    CheckboxBoolean birthday;
    @UiField
    Slider stockLowerThanLimit;
    @UiField
    Slider futureExpenses;
    @UiField
    Slider futureInstallments;

    public NotificationConfigForm(PageConfigEnum pageConfigEnum) {
        setForm(binderForm, pageConfigEnum, "");
        Popup.resizeToSmallPopup();

        NotificationConfig notificationConfig = AuthenticateUserProvider.getUser().getEnterprise().getNotificationConfig();

        stockRupture.setValue(notificationConfig.isStockRupture());
        newPastInstallments.setValue(notificationConfig.isNewPastInstallments());
        newPastExpenses.setValue(notificationConfig.isNewPastExpenses());
        paymentRequest.setValue(notificationConfig.isPaymentRequest());
        birthday.setValue(notificationConfig.isClientBirthDays());
        stockLowerThanLimit.setValue(notificationConfig.getStockLowerThanLimit());
        futureExpenses.setValue(notificationConfig.getFutureExpenses());
        futureInstallments.setValue(notificationConfig.getFutureInstallments());
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {

        NotificationConfig notificationConfig = AuthenticateUserProvider.getUser().getEnterprise().getNotificationConfig();
        notificationConfig.setStockRupture(stockRupture.getValue());
        notificationConfig.setNewPastExpenses(newPastExpenses.getValue());
        notificationConfig.setNewPastInstallments(newPastInstallments.getValue());
        notificationConfig.setPaymentRequest(paymentRequest.getValue());
        notificationConfig.setClientBirthDays(birthday.getValue());
        notificationConfig.setStockLowerThanLimit(stockLowerThanLimit.getValue());
        notificationConfig.setFutureInstallments(futureInstallments.getValue());
        notificationConfig.setFutureExpenses(futureExpenses.getValue());

        return notificationConfig;
    }

    @Override
    public boolean validate() {
        notificationServiceAsync.saveNotificationConfig((NotificationConfig) getEntity(), new IabakoAsyncCallback<Void>() {
            public void onSuccess(Void result) {
                popup.handleClickClose();
                ValidationMessage.getFromRootPage().showSuccessMessage("notification_config_save");
                AuthenticateUserProvider.getNotificationPanel().reload();
                super.onSuccess(result);
            }
        });
        return false;
    }
}
